package com.test.dazuoye.controller;

import com.test.dazuoye.domain.YongHu;
import com.test.dazuoye.service.YongHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sound.midi.Soundbank;
import java.util.List;

/**
 * @date 2020/6/6-21:47
 * 注册功能（保存用户），增加用户，删除用户
 */
@Controller
@RequestMapping("/yonghu")
public class YongHuController {

    @Autowired
    private YongHuService yongHuService;

    //查询用户
    @RequestMapping("/findall")
    public @ResponseBody
    List<YongHu> findAll() {
        List<YongHu> all = yongHuService.findAll();
        for (YongHu a : all) {
            System.out.println(a);
        }
        return all;
    }

    //注册
    @RequestMapping(value = "/zhucebt")

    public @ResponseBody
    void ZhuCeBt(YongHu yongHu) {
        System.out.println(yongHu);
        yongHuService.saveYongHu(yongHu);
        System.out.println("保存成功");
    }

    //删除用户
    @RequestMapping("/deleteyonghu")
    public @ResponseBody
    void deleteYongHu(String uname) {
        System.out.println(uname);
        if (uname != null) {
            yongHuService.deleteYongHu(uname);
            System.out.println("删除完毕:" + uname);
        } else {
            System.out.println("删除失败:" + uname);
        }
    }

    @RequestMapping(value = "/denglubt")
    public String DengLuBt(YongHu yongHu) {

        System.out.println(yongHu);
        YongHu one = yongHuService.findOne(yongHu.getUname());
        if (one != null) {
            if (yongHu.getUpassword().equals(one.getUpassword())) {

                System.out.println("验证成功");
                return "yonghu";
            } else {
                System.out.println("登录失败");
                return "errors";
            }
        }else {
            return "errors";
        }
    }

}
